/**
 * 一些子模块的初始化
 * @author Levin
 * @version 1.0
 * @dependency jquery.vPreload jquery.slimAccordion
 */
 Narato.M("core",(function($){
	var p={},pub={};
	/* crap browser wanring
	----------------------*/
	p.browserTip={
		init:function(){
			var tip='<div id="wdg-ie6Tip" class="crap-browser-warning">'+p.opts.lang_ie6+'</div>';
			if (isIE6) {
				$('body').prepend(tip);
			}
		}
	};
	/* superfish navigation
	----------------------*/
	p.superfish={
		init:function(){
			if($.fn.superfish){
				$('.navigation').superfish({ autoArrows: true });
			};
		}
	};
	/* lightbox
	-----------------------*/
	p.lightbox={
		init:function(){
			if(!p.opts.lightbox){
				return;
			}
			//enable fancyBox for any link with rel="lightbox" and on links which references end in image extensions (jpg, gif, png) ?>
			$("a[rel='lightbox'], a[href$='.jpg'], a[href$='.jpeg'], a[href$='.gif'], a[href$='.png'], a[href$='.JPG'], a[href$='.JPEG'], a[href$='.GIF'], a[href$='.PNG']").fancybox({
				'zoomSpeedIn': 333,
				'zoomSpeedOut': 333,
				'zoomSpeedChange': 133,
				'easingIn': 'easeOutQuart',
				'easingOut': 'easeInQuart',
				'overlayShow': true,
				'overlayOpacity': 0.75
			});
		}
	};
	/* fontControl
	----------------------*/
	p.fontControl={
		init:function(){
			Narato.fontControl("#nav-extra-font", "body", 10, 18);
		}
	};
	/* tabs
	-----------------------*/
	p.tabs={
		init:function(){
			$(".post-tabs").minitabs({
				content: '.sections',
				nav: '.tabs',
				effect: 'top',
				speed: 333,
				cookies: false
			});

			$(".sidebar-tabs").minitabs({
				content: '.sections',
				nav: '.box-tabs',
				effect: 'slide',
				speed: 150
			});		
		}
	};
	/* menu effects
	------------------------*/
	p.menuEf={
		init:function(){
			$(".menuList .cat-item").bubble({
				timeout: 6000
			});

			$(".shareThis, .bubble-trigger").bubble({
				offset: 16,
				timeout: 0
			});			

			$("#pageControls").bubble({
				offset: 30
			});
			$('.menuList li a').nudge({
				property: 'padding',
				direction: 'left',
				amount: 6,
				duration: 166
			});
			$('a.nav-extra').nudge({
				property: 'marginTop',
				direction: '',
				amount: -18,
				duration: 166
			});		
		}
	};
	/* slider
	----------------------*/
	p.slider={
		init:function(){
			// fade effect
			$("#footer-blocks.withSlider").loopedSlider();
			/*
			$("#featured-content.withSlider").loopedSlider({
				autoStart: p.opts.interval_slider,
				autoHeight: false
			});
			*/
			$("#featured-content.withSlider").slimSlider({
				easing:'easeInBounce',
				duration:1000
			});
		}
	}; 

	/* thumb shuffle 
	-----------------------*/
	p.thumbShuffle={
		init:function(){
			var html='',
				meta,
				imgs=[],
				cnt=$("#main .post").length;
				
			$("#main .post").each(function(i,o){
				meta=$(o).data("meta");
				if(!meta){
					return;
				};
				imgs.push(meta.thumb[0]);
				html+='<a target="_blank" href="'+meta.url+'">';
				if(i>cnt/2){
					html+='<span class="thumb-title thumb-title-l wdg-acd-bd">'+meta.title+'</span>';;
				};
				html+='<img data-src="'+meta.thumb[0]+'" width="'+meta.thumb[1]+'" height="'+meta.thumb[2]+'" alt="'+meta.title+'"/>';
				if(i<=cnt/2){
					html+='<span class="thumb-title wdg-acd-bd">'+meta.title+'</span>';;
				};
				html+='</a>';
			});
			meta=null;
			
			this.preload(imgs,$("#wdg-thumbs").html(html).find('img'));

			//accordion effect
			if(imgs.length>1){
				$("#wdg-thumbs").slimAccordion();
			}else{
				$("#wdg-thumbs").slideUp();
			};

		},//init
		preload:function(imgs,$imgs){
			if(!imgs || imgs.length==0) return;

			var showIt=function($items,src){
				$items.each(function(i,o){
					if(src.indexOf(o.getAttribute('data-src'))!==-1){
						$(o).removeAttr("data-src").attr("src",src).addClass("thumb-loaded");
						return false;
					}
				});
			};

			$.preload(imgs,{
				noCache:true,
				loaded:function(img, loaded, total){
					showIt($imgs,img.src);
				},
				loaded_all:function(){
					$("#wdg-thumbs").addClass("thumbs-done");
					var cnt=$imgs.length,
						idx=0,
						fadeIt=function(){
							if(idx>=cnt) return;
							$imgs.eq(idx).fadeIn("normal",function(){
								idx++;
								fadeIt();
							});
						};
					fadeIt();
				}
			});
		}
	};

	/* go to top
	----------------------*/
	p.goTop={
		init:function(){
			$("#goTop").click(function () {
				$('html').animate({
						scrollTop: 0
					},
				'slow');
			});		
		}
	};
	/* clear field
	-----------------------*/
	p.clearField={
		init:function(){
			$('.clearField').clearField({
				blurClass: 'clearFieldBlurred',
				activeClass: 'clearFieldActive'
			});		
		}
	};
	/* print
	-----------------------*/
	p.print={
		init:function(){
			$('.print').click(function() {
			$('.post.single').printElement({printMode:'popup'});
				return false;
			});		
		}
	};
	/* show author info
	----------------------*/
	p.authorInfo={
		init:function(){
			$("#show-author-info").livequery("click", function () {
				$("#author-info").slideFade('toggle',333,'easeOutQuart');
			});		
		}
	};
	/* accessibility
	-----------------------*/
	p.accessibility={
		init:function(){
			// set accessibility roles on some elements trough js (to not break the xhtml markup)
			$(".navigation").attr("role", "navigation");
			$(".col-m").attr("role", "main");
			$(".col-aside").attr("role", "complementary");
			$("#search").attr("role", "search");		
		}
	};
	/* preview
	----------------------*/
	p.preview={
		init:function(){
			if(p.opts.isPreview){
				$('body').addGrid(12, {img_path: p.opts.url_theme+'/admin/images/'});
			}
		}
	};
	/* jp-playlist-player fix
	----------------------*/
	p.jplayerfix={
		init:function(){
			$(".jp-playlist-player").css({"padding-bottom":0});
		}
	};

	/* tagcloud
	----------------------*/
	p.tagcloud={
		init:function(){
			var $items=$.makeArray($(".tagcloud a"));
			$items.sort(function(a,b){
				a=parseInt(a.getAttribute("data-cnt"));
				b=parseInt(b.getAttribute("data-cnt"));
				return (b-a);
			});
			var x="",j=0,cnt;
			for(var i=0;i<$items.length-1;i++){
				cnt=$items[i].getAttribute("data-cnt");
				if(x!==cnt){
					j++;
					
				};
				$($items[i]).addClass("tcweight_"+j);
				x=cnt;
			}
		}
	};

	/* public methods
	----------------------*/
	pub.init=function(opts){
		p.opts=opts;
	};
	pub.onLoad=function(){
		p.browserTip.init();
		p.superfish.init();
		Narato.webshot(".websnapr", "webshot");
		p.lightbox.init();
		p.fontControl.init();
		Narato.webshot(".websnapr", "webshot");
		p.tabs.init();
		p.menuEf.init();
		p.slider.init();
		p.goTop.init();
		p.clearField.init();
		p.print.init();
		p.authorInfo.init();
		p.accessibility.init();
		p.preview.init();	
		p.thumbShuffle.init();
		p.jplayerfix.init();
		p.tagcloud.init();
	};
	return pub;

})(jQuery));